{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2f67f9b8",
   "metadata": {},
   "outputs": [],
   "source": [
    "# !py -m pip install opencv-python\n",
    "# !python.exe -m pip install --upgrade pip\n",
    "import numpy as np\n",
    "import cv2 as cv"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "d09ba5a5",
   "metadata": {},
   "outputs": [],
   "source": [
    "def afinize(tacka):\n",
    "    return list(map(lambda x : x / tacka[2], tacka))[:-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2ba339c3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def cross(x, y):\n",
    "    return np.cross(x, y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "13a57ee9",
   "metadata": {},
   "outputs": [],
   "source": [
    "def tacka_nedogleda(t1, t2, t3, t4):\n",
    "    return list(cross(cross(t1, t2), cross(t3, t4)))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "2d00efbf",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "# def homogenize(lista_tacaka):\n",
    "#     for tacka in lista_tacaka:\n",
    "#         tacka = tacka.append(1)\n",
    "#     return lista_tacaka\n",
    "\n",
    "def osmoteme(lista_tacaka):\n",
    "    #pretvori u homogene\n",
    "#     lista_tacaka = homogenize(lista_tacaka)\n",
    "    #izracunaj_tacku\n",
    "    t1, t2, t3, t5, t6, t7, t8 = lista_tacaka\n",
    "    \n",
    "    xb = tacka_nedogleda(t2, t6, t1, t5)\n",
    "    yb = tacka_nedogleda(t5, t6, t7, t8)\n",
    "    return afinize(tacka_nedogleda(t8, xb, t3, yb))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "9390c8b2",
   "metadata": {},
   "outputs": [],
   "source": [
    "ts = []\n",
    "brojac = 0\n",
    "def click_event(event, x, y, flags, params): \n",
    "    global brojac\n",
    "    font = cv.FONT_HERSHEY_SIMPLEX \n",
    "\n",
    "    if event == cv.EVENT_LBUTTONDOWN: \n",
    "        cv.putText(slika, str(x) + ',' +\n",
    "                    str(y), (x,y), font, \n",
    "                    1, (255, 0, 0), 2) \n",
    "        cv.imshow(\"kutija\", slika)\n",
    "        \n",
    "        ts.append([x, y, 1])\n",
    "        brojac += 1\n",
    "        \n",
    "        if brojac == 7:\n",
    "            osma_tacka = osmoteme(ts)\n",
    "            cv.putText(slika, str(osma_tacka[0]) + ',' + str(osma_tacka[1]), (int(osma_tacka[0]),int(osma_tacka[1])), font, 1, (0, 255, 0), 2)\n",
    "            cv.imshow(\"kutija\", slika)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "ffaf39ad",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Warning: Ignoring XDG_SESSION_TYPE=wayland on Gnome. Use QT_QPA_PLATFORM=wayland to run on Wayland anyway.\n"
     ]
    }
   ],
   "source": [
    "putanja = './kutija/kutija_ppgr.jpg'\n",
    "slika = cv.imread(putanja)\n",
    "\n",
    "cv.imshow(\"kutija\", slika)\n",
    "\n",
    "cv.setMouseCallback(\"kutija\", click_event)\n",
    "\n",
    "cv.waitKey(0)\n",
    "cv.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "868379e3",
   "metadata": {},
   "outputs": [],
   "source": [
    "ts = [[595, 301, 1],\n",
    "     [292, 517, 1],\n",
    "     [157, 379, 1],\n",
    "     [665, 116, 1],\n",
    "     [304, 295, 1],\n",
    "     [135, 163, 1],\n",
    "     [509, 43, 1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1e2eee92",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
